﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using BLL;
using Model;
namespace UI
{
    public partial class 登录界面 : Form
    {
        public 登录界面()
        {
            InitializeComponent();
        }
       
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                textBox2.PasswordChar = '\0';//显示密码以实际结果展示
            }
            else
            {
                textBox2.PasswordChar ='*';//隐藏密码
            }
        }
        //记住密码
        public void show()
        {
            //获取根节点
            XmlNode node = docum.DocumentElement;
            //判断是否被选中
            if (checkBox2.Checked)
            {
                foreach (XmlNode item in node.ChildNodes)
                {
                    //条件成立就执行
                    if (item["Uid"].InnerText != textBox1.Text)
                    {
                        item["Uid"].InnerText = textBox1.Text;
                        item["Pwd"].InnerText = textBox2.Text;
                        //保存
                        docum.Save("../../XMLFile1.xml");
                    }
                }
            }
            else
            {
                foreach (XmlNode item in node.ChildNodes)
                {
                    //条件成立就执行
                    if (item["Uid"].InnerText == textBox1.Text)
                    {
                        item["Uid"].InnerText = "";
                        item["Pwd"].InnerText = "";
                        //保存
                        docum.Save("../../XMLFile1.xml");
                    }
                }
            }
        }
        //实例化xmldoc对象
        XmlDocument docum = new XmlDocument();
        private void Form1_Load(object sender, EventArgs e)
        {

            //加载xml文件
            docum.Load("../../XMLFile1.xml");
            //获取根节点
            XmlNode node = docum.DocumentElement;
            foreach (XmlNode item in node.ChildNodes)
            {
                //条件成立就执行
                if (item["Uid"].InnerText !="")
                {
                    textBox1.Text = item["Uid"].InnerText;
                    textBox2.Text = item["Pwd"].InnerText;
                    checkBox2.Checked = true;
                }
            }
            //加载默认调用失焦
            textBox1_Leave(null, null);
           
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //关闭窗体
            Application.Exit();
        }
       
        private void button1_Click(object sender, EventArgs e)
        {
            //获取文本数据
            string uid = textBox1.Text;
            string pwd = textBox2.Text;
            //非空判断
            if (uid =="" || uid == "账号")
            {
                MessageBox.Show("请填写账号");
                return;
            }
            //非空判断
            if (pwd == "" || pwd =="密码")
            {
                MessageBox.Show("请填写密码");
                return;
            }
            //传值
            Admins.UserID = int.Parse(uid);
            Admins.UserPwd = pwd;
            //执行操作
            object o =  登录界面BLL.Denglu();
            //判断结果
            if (Convert.ToInt32(o) > 0)
            {
                //登录成功调用方法
                show();
                //获取当前账号的真实姓名
                DataSet da = 登录界面BLL.YonghuName();
                foreach (DataRow item in da.Tables[0].Rows)
                {
                    //获取行的列名获取数据
                    Admins.UserCompellation = item["UserCompellation"].ToString();
                }
                //跳转
                主界面 f = new 主界面();
                f.Show();
                this.Hide();
            }else
            {
                MessageBox.Show("登录失败");
            }
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar !=8)
            {
                if (!(char.IsDigit(e.KeyChar)))
                {
                    MessageBox.Show("请输入数字");
                    e.Handled = true;
                }
            }
        }

        private void label4_Click(object sender, EventArgs e)
        {
            修改操作 f = new 修改操作();
            f.Show();
            this.Hide();
        }
        //获取焦点
        private void textBox1_Enter(object sender, EventArgs e)
        {
            if (textBox1.Text == "账号")
            {
                textBox1.Text = "";
                //修改textBox控件颜色系统的颜色
                textBox1.ForeColor = SystemColors.WindowText;
            }
        }
        //失去焦点
        private void textBox1_Leave(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                textBox1.Text = "账号";
                //修改textBox控件颜色
                textBox1.ForeColor = Color.Gray;
            }
        }
        //失去焦点
        private void textBox2_Leave(object sender, EventArgs e)
        {
            //判断条件
            if (textBox2.Text == "")
            {
                textBox2.Text = "密码";
                //修改textBox控件颜色
                textBox2.ForeColor = Color.Gray;
                //失焦显示
                textBox2.PasswordChar = '\0';
            }
        }
        //获取焦点
        private void textBox2_Enter(object sender, EventArgs e)
        {
            //判断条件
            if (textBox2.Text == "密码")
            {
                textBox2.Text = "";
                //修改textBox控件颜色
                textBox2.ForeColor = SystemColors.WindowText;
                //失焦隐
                textBox2.PasswordChar = '*';
            }
        }
        //鼠标移动
        private void label4_MouseEnter(object sender, EventArgs e)
        {
            label4.ForeColor = Color.Red;
        }
        //鼠标移出
        private void label4_MouseLeave(object sender, EventArgs e)
        {
            label4.ForeColor = Color.Blue;
        }

    }
}
